<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="12">

          <a-col :md="4" :sm="8">
            <a-form-item label="车牌号">
              <a-input placeholder="请输入车牌号" v-model="queryParam.plateNo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>

            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('已审核访客记录')">导出</a-button>
    </div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">查看</a>
        </span>

      </a-table>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <VisitorRecordAuditedListModal ref="modalForm" @ok="modalFormOk"></VisitorRecordAuditedListModal>
    <VisitorRecordModal ref="modalAddForm" @ok="modalFormOk"></VisitorRecordModal>
  </a-card>
</template>

<script>
import VisitorRecordAuditedListModal from './modules/VisitorRecordAuditedListModal'
import VisitorRecordModal from './modules/VisitorRecordModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import {deleteAction, getAction} from '@/api/manage'
import { filterObj } from '@/utils/util';

export default {
  name: "VisitorRecordList",
  mixins:[JeecgListMixin],
  components: {
    VisitorRecordAuditedListModal,
    VisitorRecordModal
  },
  data () {
    return {
      description: '访客记录表管理页面',
      // 表头
      columns: [
        {
          title: '车牌号',
          align:"center",
          dataIndex: 'plateNo'
        },
        {
          title: '停车场编号',
          align:"center",
          dataIndex: 'parkCode'
        },
        {
          title: '停车场名称',
          align:"center",
          dataIndex: 'parkName'
        },
        {
          title: '客户姓名',
          align:"center",
          dataIndex: 'userName'
        },
        {
          title: '客户手机号码',
          align:"center",
          dataIndex: 'mobile'
        },
        {
          title: '家庭住址',
          align:"center",
          dataIndex: 'address'
        },
        {
          title: '起始时间',
          align:"center",
          dataIndex: 'startTime'
        },
        {
          title: '结束时间',
          align:"center",
          dataIndex: 'endTime'
        },
        {
          title: '申请时间',
          align:"center",
          dataIndex: 'createTime'
        },
        {
          title: '审核人',
          align:"center",
          dataIndex: 'auditUserName'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          scopedSlots: { customRender: 'action' },
        }
      ],
      url: {
        list: "/visitors/visitorRecord/list",
        delete: "/visitors/visitorRecord/delete",
        deleteBatch: "/visitors/visitorRecord/deleteBatch",
        exportXlsUrl: "visitors/visitorRecord/exportXls",
        importExcelUrl: "visitors/visitorRecord/importExcel",
      },
      queryParam: {
        "type":"-1",
      },
      isorter:{
        column: 'createTime',
        order: 'desc',
      },
    }
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
  created(){

    this.keyupEnter()
  },
  methods: {
    handleAdd: function () {
      this.$refs.modalAddForm.add();
      this.$refs.modalAddForm.title = "新增";
      this.$refs.modalAddForm.disableSubmit = false;
    },
    handleDetail: function (record) {
      this.$refs.modalForm.edit(record);
      this.$refs.modalForm.title = "详情";
      this.$refs.modalForm.disableSubmit = true;
    },

    searchQuery() {
      this.loadData(1);
    },

    getQueryParams(){

      var param = Object.assign({}, this.isorter, this.queryParam);
      param.field = this.getQueryField();
      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      return filterObj(param);
    },

    loadData(arg) {
      if(!this.url.list){
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();//查询条件
      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
        this.loading = false;
      })
    },

    batchDel: function () {

      if(!this.url.deleteBatch){
        this.$message.error("请设置url.deleteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectionRows.length; a++) {
          ids += this.selectionRows[a].invateuserdetailId + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              } else {
                that.$message.warning(res.message);
              }
            });
          }
        });
      }
    },
    handleDelete: function (invateuserdetailId) {
      if(!this.url.delete){
        this.$message.error("请设置url.delete属性!")
        return
      }
      var that = this;
      deleteAction(that.url.delete, {invateuserdetailId: invateuserdetailId}).then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          that.loadData();
        } else {
          that.$message.warning(res.message);
        }
      });
    },


    keyupEnter(){
      document.onkeydown = e =>{
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13  ) {
          this.loadData(1);
        }
      }
    },

  }
}
</script>
<style scoped>
@import '~@assets/less/common.less'
</style>
